/**
 * @description User路由
 * @author malin
 */

const router = require('koa-router')()
const { register, login } = require('../../controllers/user')
const genValidator = require('../../middlewares/validator')
const userValidator = require('../../validator/user')

router.prefix('/api/user')

// 用户注册
router.post('/register', genValidator(userValidator), async (ctx, next) => {
    // 获取post请求数据
    const { userName, password, gender } = ctx.request.body
    // 调用controller并返回数据
    ctx.body = await register({ userName, password, gender })
})

// 用户登录
router.post('/login', genValidator(userValidator), async (ctx, next) => {
    const { userName, password } = ctx.request.body
    ctx.body = await login(userName, password)
})

module.exports = router
